package com.infore.siteManagement.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.infore.siteManagement.outEntity.SysUserStation;
import com.infore.siteManagement.outEntity.TPlatformCustomerStation;
import com.infore.siteManagement.repository.ISysUserStationRepository;
import com.infore.siteManagement.repository.ITPlatformCustomerStationRepository;
import com.infore.siteManagement.service.IStationSystemService;

@Service
public class StationSystemServiceImpl implements IStationSystemService{

	@Autowired
	private ITPlatformCustomerStationRepository platformCustomerStationRepository;
	@Autowired
	private ISysUserStationRepository sysUserStationRepository;

    public int addPlatformCustomerStations(String platformId, String[] stationIdsArr) {
        platformCustomerStationRepository.delByPlatformId(platformId);
        List<TPlatformCustomerStation> list = new ArrayList<>();
        TPlatformCustomerStation platformStation = null;
        for (String stationId : stationIdsArr) {
            platformStation = new TPlatformCustomerStation();
            platformStation.setPlatformId(platformId);
            platformStation.setStationId(stationId);
            list.add(platformStation);
        }
        platformCustomerStationRepository.saveAll(list);
        return 0;
    
    }

    public List<TPlatformCustomerStation> getPlatformCustomerStations(String platformId) {
        return platformCustomerStationRepository.findByPlatformId(platformId);
    }
    
    public int addUserStations(String userId, String[] stationIds) {
//        SysUser sysUser = getUserById(userId);
//        if (sysUser == null) {
//            return -1;    //角色不存在
//        }
        sysUserStationRepository.delByUserId(userId);
        List<SysUserStation> list = new ArrayList<>();
        SysUserStation userStation = null;
        for (String stationId : stationIds) {
            userStation = new SysUserStation();
            userStation.setUserId(userId);
            userStation.setStationId(stationId);
            list.add(userStation);
        }
        sysUserStationRepository.saveAll(list);
        return 0;
    
    }

    public List<SysUserStation> getUserStations(String userId) {
        return sysUserStationRepository.findByUserId(userId);
    }
}
